package data.store;

public class DataWarehouse {

    private static RegisterNode<PartSpace> partRegisterNode = new RegisterNode<>();
    private static RegisterNode<LineItemSpace> lineItemRegisterNode = new RegisterNode<>();

    private static RegisterNode<PartSpace> lastPartNode = partRegisterNode;
    private static RegisterNode<LineItemSpace> lastLineItemNode = lineItemRegisterNode;

    public synchronized static PartSpace allocationPartSpace(){
        PartSpace partSpace = new PartSpace();
        RegisterNode<PartSpace> newNode = new RegisterNode<>(partSpace);
        lastPartNode.next = newNode;
        lastPartNode = newNode;
        return partSpace;
    }

    public synchronized static LineItemSpace allocationLineItemSpace(){
        LineItemSpace lineItemSpace = new LineItemSpace();
        RegisterNode<LineItemSpace> newNode = new RegisterNode<>();
        lastLineItemNode.next = newNode;
        lastLineItemNode = newNode;
        return lineItemSpace;
    }

    public static RegisterNode<PartSpace> getFirstPartRegisterNode(){
        return partRegisterNode;
    }

    public static RegisterNode<LineItemSpace> getFirstLineItemRegisterNode(){
        return lineItemRegisterNode;
    }

    static class RegisterNode<T> {
         T value;
         RegisterNode<T> next;

         RegisterNode(){
            value = null;
        }
         RegisterNode(T value){
            this.value = value;
        }
    }
}
